package com.kdgc.power.chess.controller;

import com.kdgc.power.chess.entity.ChSqyq;
import com.kdgc.power.chess.service.ChSqyqService;
import com.kdgc.power.common.util.StringUtil;
import com.kdgc.power.fw.bean.JsonMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("ChSqyq")
public class ChSqyqController {
    @Autowired
    private ChSqyqService chSqyqService;

    @ResponseBody
    @RequestMapping("page")
    public Object getPage(HttpServletRequest request, int page, int rows) {
        String fuzzy = request.getParameter("fuzzy");
        String chessId = request.getParameter("chessId");
        String mark = request.getParameter("mark");
        Map<String, Object> para = new HashMap<>();
        if (StringUtil.isNotEmpty(fuzzy)) {
            para.put("fuzzy", "%" + fuzzy + "%");
        }
        if (StringUtil.isNotEmpty(chessId)) {
            para.put("chessId", chessId);
        }
        if (StringUtil.isNotEmpty(mark)) {
            para.put("mark", mark);
        }
        return chSqyqService.getPage("chSqyq.select", para, page, rows);
    }

    @ResponseBody
    @RequestMapping("findById")
    public Map<String, Object> findById(Integer id) {
        return chSqyqService.getMapEntity(id);
    }

    @ResponseBody
    @RequestMapping("save")
    public JsonMsg save(ChSqyq entity) {
        boolean success = chSqyqService.saveOrUpdate(entity);
        return new JsonMsg(success);
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public JsonMsg deleteById(Integer id) {
        int rows = chSqyqService.removeById(id);
        return new JsonMsg(rows == 1);
    }
}